package com.monkeygroup.mygoplus.http;

import android.util.Log;

import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.monkeygroup.mygoplus.base.PersonModel;
import com.monkeygroup.mygoplus.base.ThingsModel;

import java.util.Iterator;
import java.util.Map;

/**
 * Created by cumt on 2017/7/13.
 */

public class PersonHttpModel {

    private SimpleOnHttpRequestListener mRequestListener;

    protected CustomStringRequest mJsonRequest;
    protected String mRequestUrl = "";
    protected boolean isTokenSave = false;
    protected Map<String, String> mRequestParams = null;
    protected int mRequestMethod = Request.Method.GET;
    public final static int TIMEOUTMS = 30000;

    /***
     * 网络请求GET方式,token参数默认传true
     ****/
    public PersonHttpModel(String url, SimpleOnHttpRequestListener listener, boolean isTokenSave) {
        Log.e("urlurl===",url);
        this.mRequestListener = listener;
        this.mRequestUrl = url;
        this.isTokenSave = isTokenSave;
    }

    /**
     * 请求信息
     */
    public void requestUserInfo() {
        mJsonRequest = new CustomStringRequest(mRequestMethod, mRequestUrl,
                mRequestParams, new Response.Listener<String>() {

            @Override
            public void onResponse(String response) {

                Iterator<Map.Entry<String, String>> iterator = mRequestParams.entrySet().iterator();
                while (iterator.hasNext()) {
                    Map.Entry<String, String> entry = iterator.next();

                    Log.e("請求參數==","key = " + entry.getKey() + " and value = " + entry.getValue());
                }

                PersonModel resultModel = handleResponse(response);
                Log.e("结果-->","status:"+resultModel.status);
                mRequestListener.onRequestFinished(resultModel.status,resultModel.seq,resultModel.isbind,resultModel.acc);
            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                mRequestListener.onRequestFinished("ERROR_UNCONNECT",0);
            }
        });

        // 设置网络超时时间
        mJsonRequest.setRetryPolicy(new DefaultRetryPolicy(TIMEOUTMS, 1, 1.0f));
        HttpRequestManager.addHttpRequest(mJsonRequest);
    }

    /**
     * 请求信息
     */
    public void requestUserLogin() {
        mJsonRequest = new CustomStringRequest(mRequestMethod, mRequestUrl,
                mRequestParams, new Response.Listener<String>() {

            @Override
            public void onResponse(String response) {

                StringBuffer str = new StringBuffer("");
                Iterator<Map.Entry<String, String>> iterator = mRequestParams.entrySet().iterator();
                while (iterator.hasNext()) {
                    Map.Entry<String, String> entry = iterator.next();
                    str.append(entry.getKey()+"=" + entry.getValue());
                    str.append("&");
//                    Log.e("請求參數==","key = " + entry.getKey() + "&" + entry.getValue());
                }
                Log.e("strstr===",str.toString());
                PersonModel resultModel = handleResponse(response);
                Log.e("结果-->","status:"+resultModel.status);
                mRequestListener.onRequestFinished(resultModel.status,resultModel.seq,resultModel.flag,resultModel.flag_name,resultModel.getSecflag(),resultModel.getSecflag_name());
            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                mRequestListener.onRequestFinished("ERROR_UNCONNECT",0);
            }
        });

        // 设置网络超时时间
        mJsonRequest.setRetryPolicy(new DefaultRetryPolicy(TIMEOUTMS, 1, 1.0f));
        HttpRequestManager.addHttpRequest(mJsonRequest);
    }

    /****
     * 网络请求POST方式,token参数默认传true
     ****/
    public PersonHttpModel(String url, Map<String, String> params, SimpleOnHttpRequestListener listener, boolean isTokenSave) {
        Log.e("urlurl===",url);
        this.mRequestListener = listener;
        this.mRequestUrl = url;
        this.mRequestParams = params;
        this.mRequestMethod = Request.Method.POST;
        this.isTokenSave = isTokenSave;
    }

    private PersonModel handleResponse(String jsonString) {
        Gson gson = new Gson();
        Log.e("jsonStringjsonString==",""+jsonString);
        PersonModel model = gson.fromJson(jsonString, new TypeToken<PersonModel>() {
        }.getType());
        Log.e("result::::", ""+model.getStatus());
        return model;
    }

    public interface SimpleOnHttpRequestListener {
        void onRequestFinished(String resultCode, Integer timestamp);
        void onRequestFinished(String resultCode,int seq,int isbind,String acc);
        void onRequestFinished(String resultCode,int seq,int flag,String flag_name,int secflag,String secflag_name);
    }



}
